package jvm.autoMemoryManagement;

/**
 * @author msi
 * 代码清单2-1 HotSpot解释器代码片段
 */
public class HotSpotByteCodeInterpreter {
    /*
    public static void main(String[] args) {
        //确保常量池中存放的是已解释的类
        if(!constants->tag_at(index).is_unresolved_klass()){
            //断言确保是klassOop和instanceKlassOop
            opp entry = (klassOop) *constants->obj_at_addr(index);
            assert(entry->is_klass(). "Should be resolved klass");
            klassOop k_entry = (klassOop) entry;
            assert(k_entry->klass_part()->oop_is_instance(),"Should be instanceKlass");
            instanceKlass* ik = (instanceKlass) k_entry->klass_part();
            //确保对象所属类型已经经过初始化阶段
            if(ik->is_initialized() && ik->can_be_fastpath_allocated() ){
                //获取对象长度
                size_t obj_size = ik->size_helper();
                oop result = NULL;
                //记录是否需要将对象的所有字段置零值
                bool need_zero = !ZeroTLAB;
                //是否在TLAB分配对象
                if(UseTLAB){
                    result= (oop) THREAD->tlab().allocate(obj_size);
                }
                if(result==NULL){
                    need_zero = true;
                    //直接在eden中分配对象
                retry:
                    HeapWord* compare_to = *Universe::heap()->top_addr();
                    HeapWord* new_top = compare_to + obj_size;
                    //cmpxchg是x86中的CAS指令，这是一个c++方法，通过CAS分配空间，如果并发失败则回到retry位置继续分配。
                    if(new_top <= *Universe::heap()->top_addr()){
                        if(Atomic::cmpxchg_ptr(new_top, Universe::heap()->top_addr(), compare_to) != compare_to){
                            goto retry;
                        }
                        result = (oop) compare_to;
                    }
                }
                if(result != NULL){
                    //如果需要，为对象初始化零值
                    if(need_zero){
                        HeapWord* to_zero = (HeapWord*) result + sizeof(oopDesc) / oopSize;
                        obj_size -= sizeof(oopDesc) / oopSize;
                        if(obj_size>0) {
                            memset(to_zero, 0, obj_size * HeapWordSize);
                        }
                    }
                    //根据是否启用偏向锁，设置对象头信息
                    if(UseBiasedLocking){
                        result->set_mark(ik->prototype_header());
                    }else{
                        result->set_mark(markOopDesc::prototype());
                    }
                    result->set_klass_gap(0);
                    result->set_klass(k_entry);
                    //将对象引用入栈，继续执行下一条指令
                    SET_STACK_OBJECT(result, 0);
                    UPDATE_PC_AND_TOS_AND_CONTINUE(3,1);
                }
            }
        }
    }
     */
}

/**
 * @author msi
 * 代码清单2-2 MarkOop.hpp 片段
 */
class MarkOOP{
    //Bit-format of an object header (most significant first, big endian layout below):
    //
    //32 bits:
    //-------
    //hash:25 ----------->| age:4   biased_lock:1 lock:2 (normal object)
    //JavaThread*:23 epoch:2 age:4  biased_lock:1 lock:2 (biased object)
    //size:32 ------------------------------------------------->|  (CMS free block)
    //PromotedObject*:29 ----------->| promo_bits:3 ----------->|  (CMS promoted object)
}